// pages/car/car.js
import {
  shoppingCarList,
  shoppingCarAddModify,
  userAddressList,
  shoppingCarDelete
} from '../../api/api'
import {
  region
} from '../../api/region'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    //默认地址
    address: '',
    //购物车列表
    list: [],
    //总价
    total: 0,
    //选择的shop_car_id数组
    choose: [],
    //选中的商品数组
    chooseCommodityArr: [],
    //消息弹窗
    dialogShow: false,
    buttons: [{
      text: '取消'
    }, {
      text: '确定'
    }],
    delId: -1,
    //顶部提示
    topType: 'success',
    topInfo: '',
    showTop: false,
    allChoose:false
  },
  delShop(e) {
    console.log(e);
    this.setData({
      delId: e.currentTarget.dataset.index,
      dialogShow: true
    })
    
  },
  tapDialogButton(e) {
    console.log(e);
    if (e.detail.item.text == "确定") {
      shoppingCarDelete({
        token: app.globalData.userInfo.token,
        shopping_car_id: this.data.delId
      }).then(res => {
        console.log(res);
        this.setData({
          topType: 'info',
          topInfo: res.msg,
          showTop: true
        })
        this.getShopCar()
      })
    }
    this.setData({
      dialogShow: false,
    })
  },
  //商品加
  red(e) {
    if (e.currentTarget.dataset.item.num <= 1) {
      return
    }
    console.log(e.currentTarget.dataset.item.price);
    console.log(e.currentTarget.dataset.item.num);
    let money = e.currentTarget.dataset.item.price * (e.currentTarget.dataset.item.num - 1)
    console.log(money);
    shoppingCarAddModify({
      token: app.globalData.userInfo.token,
      good_id: e.currentTarget.dataset.item.good_id,
      num: --e.currentTarget.dataset.item.num,
      price: e.currentTarget.dataset.item.price,
      money,
      sku: e.currentTarget.dataset.item.sku,
      shopping_car_id: e.currentTarget.dataset.item.shopping_car_id
    }).then(res => {
      this.getShopCar()
    })
    this.count()
  },
  add(e) {
    console.log(this.data.choose);
    console.log(e);
    shoppingCarAddModify({
      token: app.globalData.userInfo.token,
      good_id: e.currentTarget.dataset.item.good_id,
      num: e.currentTarget.dataset.item.num+1,
      price: e.currentTarget.dataset.item.price,
      money: e.currentTarget.dataset.item.price * (e.currentTarget.dataset.item.num+1),
      sku: e.currentTarget.dataset.item.sku,
      shopping_car_id: e.currentTarget.dataset.item.shopping_car_id
    }).then(res => {
      this.getShopCar()
    })
    this.count()
  },
  //去结算
  goOrder() {
    let obj = {
      shopping_car_id: this.data.choose,
      money: this.data.total,
      chooseCommodityArr: this.data.chooseCommodityArr
    }
    wx.navigateTo({
      url: '/pages/order/order?obj=' + JSON.stringify(obj)
    })
  },
  //价格计算
  count() {
    let choose = this.data.choose
    let list = this.data.list
    let indexArr = []
    let chooseCommodityArr = []
    let all = 0
    choose.forEach(item => {
      let index = list.findIndex(i => i.shopping_car_id == item)
      // console.log(index);
      if (index != -1) {
        indexArr.push(index)
      }
    })
    // console.log(indexArr);
    indexArr.forEach(j => {
      all += Number(list[j].money)
      chooseCommodityArr.push(list[j])
    })
    this.setData({
      total: all,
      chooseCommodityArr
    })
  },
  //全选
  chooseAll() {
    let arr = this.data.list
    let newArr = []
    let flag = this.data.allChoose
    console.log(flag);
    if (flag) {
      arr.forEach(item => {
        item.checked = false
      })
    } else {
      arr.forEach(item => {
        item.checked = true
        newArr.push(item.shopping_car_id)
      })
    }

    this.setData({
      list: arr,
      choose: newArr,
      allChoose:!this.data.allChoose
    })
    console.log(this.data.choose);
    this.count()
  },
  //复选框
  choose(e) {
    e.detail.value.forEach(item=>{
      this.data.list.map(k=>{
        if(k.shopping_car_id==item){
          return k.checked=true
        }
      })
    })
    let flag=false
    if(e.detail.value.length==this.data.list.length){
      flag=true
    }
    console.log(this.data.list);
    console.log(flag);
    this.setData({
      choose: e.detail.value,
      allChoose:flag
    })
    this.count()
  },
  //获取购物车列表
  getShopCar() {
    shoppingCarList({
      token: app.globalData.userInfo.token,
      page: this.data.page
    }).then(res => {
      console.log(res);
      wx.hideLoading({
        success: (res) => {},
      })
      res.data.data.forEach(item => {
        item.sku = JSON.parse(item.sku)
        item.checked = false
      })
      this.data.choose.forEach(item=>{
        res.data.data.map(i=>{
          if(item==i.shopping_car_id){
            return i.checked=true
          }
        })
      })
      this.setData({
        list: res.data.data.reverse()
      })
      this.count()
    })
  },
  getAddress() {
    userAddressList({
      token: app.globalData.userInfo.token
    }).then(res => {
      res.data.forEach(item => {
        let procince = `${item.procince}0000`
        let city = `${item.procince}${item.city}00`
        let area = `${item.procince}${item.city}${item.area}`
        let index = region.findIndex(i => i.provinceCode == procince)
        let cIndex = region[index].mallCityList.findIndex(j => j.cityCode == city)
        let aIndex = region[index].mallCityList[cIndex].mallAreaList.findIndex(k => k.areaCode == area)
        item.procince_text = region[index].provinceName
        item.city_text = region[index].mallCityList[cIndex].cityName
        item.area_text = region[index].mallCityList[cIndex].mallAreaList[aIndex].areaName
      })
      let address = res.data.filter(item => item.default)
      console.log(address);
      this.setData({
        address: address[0]
      })
      console.log(this.data.address);
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中',
    })
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getShopCar()
    this.getAddress()
    this.setData({
      choose:[],
      allChoose:false
    })
    this.count()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})